﻿games.game3 = {
    wordsInPlay: 0,
    words: "paris,newyork,london,frankfurt,harjit,dilshini,may,february,september,germany,paddington,plumstead,ealing,georgia,jackson,starbucks".split(","),
    wordNumber: 0,
    timeBetweenWords: 2000,
    score: 0,
    stringBuffer: "",
    init: function () {
        $("#gamezone").html("");
        games.helper.getRandomNumber(0, games.game3.words.length);
        alert("When your ready, click ok, then type what you see");
        $(document).keyup(function (event) {
            var alphaKey = String.fromCharCode(event.keyCode).toLowerCase();
            if ("abcdefghijklmnopqrstuvwxyz".indexOf(alphaKey) > -1) {
                games.game3.stringBuffer += alphaKey;
                $(".game3-words").each(function () {
                    var l = games.game3.stringBuffer.length;
                    var l2 = $(this).text().length;
                    if (games.game3.stringBuffer.substring(l - l2, l) == $(this).text()) {
                        games.game3.stringBuffer = "";
                        $(this).trigger("blow");
                        games.game3.wordsInPlay--;
                        games.game3.score++;
                    }
                });
            }
        });
        games.game3.addWord();
    },
    addWord: function () {
        games.game3.wordsInPlay++;
        if (games.game3.wordsInPlay < 10) {
            var word = games.game3.words[games.helper.getRandomNumber(0, games.game3.words.length)];
            $("#gamezone").append("<span class='game3-words' id='game3-word" + games.game3.wordNumber + "'>" + word + "</span>");
            $("#game3-word" + games.game3.wordNumber).bind("blow", (function () { $(this).remove(); }));
            $("#game3-word" + games.game3.wordNumber).css("top", "50px").animate({ "top": "500px" }, 4000);
            games.game3.wordNumber++;
            setTimeout(function () {
                games.game3.timeBetweenWords -= 100;
                if (games.game3.timeBetweenWords < 500) {
                    games.game3.timeBetweenWords = 500
                }
                games.game3.addWord();
            }, games.game3.timeBetweenWords);
        } else {
            alert("oh dear, you were defeated. You scored " + (games.game3.score));
        }
    }
};